<template>
  <div class="reader-dashboard-layout">
    <el-container style="height: 100vh; border: 1px solid #eee">
      <el-aside width="200px" style="background-color: rgb(238, 241, 246)">
        <el-menu router :default-active="$route.path" :default-openeds="['1']">
          <el-submenu index="1">
            <template slot="title"><i class="el-icon-setting"></i>读者导航</template>
            <el-menu-item index="/reader-dashboard/available-books">
              <i class="el-icon-reading"></i>可借阅图书
            </el-menu-item>
            <el-menu-item index="/reader-dashboard/borrow-history">
              <i class="el-icon-time"></i>借阅历史
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <el-container>
        <el-header style="text-align: right; font-size: 12px">
          <div style="display: flex; align-items: center; justify-content: flex-end; width: 100%;">
            <span style="margin-right: 15px;">欢迎，{{ username }}</span>
            <el-dropdown>
              <i class="el-icon-setting" style="margin-right: 15px; cursor: pointer;"></i>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>个人中心</el-dropdown-item>
                <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-header>

        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'ReaderDashboardLayout',
  data() {
    return {
      username: localStorage.getItem('username') || '读者',
    };
  },
  methods: {
    logout() {
      localStorage.removeItem('token');
      localStorage.removeItem('username');
      localStorage.removeItem('isAdmin');
      localStorage.removeItem('userid');
      this.$message.success('已成功注销');
      this.$router.push('/login');
    }
  },
  created() {
    // 当直接访问 /reader-dashboard 时，如果没有明确的子路由，重定向到默认的子路由
    // 这可以确保当用户直接访问 /reader-dashboard 时，有一个默认的页面内容显示
    if (this.$route.path === '/reader-dashboard' || this.$route.path === '/reader-dashboard/') {
      this.$router.replace('/reader-dashboard/available-books');
    }
  }
};
</script>

<style scoped>
.el-header {
  background-color: #B3C0D1;
  color: #333;
  line-height: 60px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 20px;
}

.el-aside {
  color: #333;
}
.reader-dashboard-layout {
  height: 100%;
  width: 100%;
}
</style>